Learn-PHP


PHP for beginners


9 User See All
Chauhan MitulbhaiParth Bhagatpatel upendraNAGA SAI HEMANTH JAMILIPalak Shah
Jitu VadherArpan PatelSHUBHAM MISHRAJoydip Panchal
Photo
Back to cluster list

Make Comments..!!


Joydip Panchal
Bubble sort in PHP :

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list.
Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.
Source from wikipedia

Example :

<?php
function bubbleSort(array $array)
{
$n = sizeof($array);
for ($i = 1; $i < $n; $i++) {
for ($j = $n - 1; $j >= $i; $j--) {
if($array[$j-1] > $array[$j]) {
$tmp = $array[$j - 1];
$array[$j - 1] = $array[$j];
$array[$j] = $tmp;
}
}
}

return $array;
}

echo "<pre>";
$array = array(200,5,26,7,55,9);
echo "Array before bubble sort : <br />";
print_r($array);
$array = bubbleSort($array);
echo "<br />";
echo "Array after bubble sort : <br />";
print_r($array);
echo "</pre>";
?>
Like · Comment ·
Joydip Panchal
During development in the situation where you need to code something like below :

<?php
$name = $array['name'];
$address = $array['address'];
$city = $array['city'];
?>

If you want to do this, you can do it with inbuilt function in PHP is "extract"
For example,

<?php
extract($array);
?>

This will automatically make $name = $array['name'];
Like · Comment ·
Arpan Patel
Image resize in PHP :
In this example we are using 4 functions
1. imagecreatetruecolor
2. imagecreatefromjpeg
3. imagecopyresized
4. imagejpeg

1. imagecreatetruecolor :
Description : returns an image identifier representing a black image of the specified size.

2. imagecreatefromjpeg :
Description : returns an image identifier representing the image obtained from the given filename.

3. imagecopyresized :
Description : copies a rectangular portion of one image to another image.

4. imagejpeg :
Description : creates a JPEG file from the given image.

Example :

$filename = 'test.jpg';
$percent = 0.5;

// Content type - If you want to outout an image into browser.
header('Content-Type: image/jpeg';

// Size of image we want to resize
$newwidth = 200;//$width * $percent;
$newheight = 200;// $height * $percent;

// Get actual width and height of image
list($width, $height) = getimagesize($filename);

// Create a black image identifier for our desired resized image
$thumb = imagecreatetruecolor($newwidth, $newheight);

// Load actual image identifier
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output in browser
imagejpeg($thumb);

// To save image
imagejpeg($thumb,'test-thumb.jpg',100);

Resource : http://php.net/manual/en/function.imagecopyresized.php
Like · Comment ·
Jitu Vadher likes this
Joydip Panchal
The main problems in using eval() are:

1 . Passing an untrusted parameter is a way to fail. It is often not a trivial task to make sure that a parameter is fully trusted.
2 . Using eval() makes code clever, therefore more difficult to follow. T

The main problem with actual use of eval() is only one:


Follow this rules if you are using eval():

1. Sometimes eval is the only/the right solution.
2. or most cases one should try something else.
3. If unsure, goto 2.
4. Else, be very, very careful.
Like · Comment ·
Jitu Vadher likes this
Arpan Patel
Good one
Like · 1 ·
Joydip Panchal
Like · Comment ·
Jitu Vadher likes this
Joydip Panchal
Simple database connection using mysqli extension.

<?php

// Connection
$link = mysqli_connect("your-host","your-username","your-password","your-db" or die("Connection Error " . mysqli_error($link));

// Select Query

$query = "SELECT your-column-name FROM your-tablename";

// Execution of query.

$result = mysqli_query($link, $query);

// Display information from result :

while($row = mysqli_fetch_array($result)) {
echo $row["your-column-name"] . "<br>";
}

?>
Like · Comment ·
Jitu Vadher likes this
Arpan Patel
Good
Like ·
Download Android App